import { getHscodeList , getHscodeById, addHscode, updateHscode, deleteHscodeById} from '../../services/basedata/customsProductCode';
import { message } from 'antd';
const R = require('ramda');

export default {
    namespace: 'customsProductCode',
    state: {
        page: {},
        list: [],
        dataSource: {}
    },
    reducers: {
        /**
         * 刷新state
         *
         * @param state
         */
        refreshState(state, { payload }){
            return {...state, ...payload };
        }
    },
    effects: {
        /**
         * 查询商品编码列表
         *
         * @param call
         * @param put
         * @param select
         */
        *getHscodeList({ payload }, { call, put, select }) {
            const res = yield call(getHscodeList, { ...payload });
            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                yield put({
                    type: 'refreshState',
                    payload: {
                        list: content.list,
                        page: content.page
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },

        /**
         * 删除商品编码
         *
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *deleteHscodeById({ payload }, { call, put, select }) {
            const res = yield call(deleteHscodeById, { id: payload.id });
            if (res.data.resultCode === "ok") {
                message.info("海关商品码删除成功！");
                if(payload.onSuccess)payload.onSuccess()
            } else {
                message.error(res.data.errMessage);
            }
        },

        /**
         * 获取商品编码详情
         *
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *getHscodeById({ payload }, { call, put }){
            if(payload.id){
                const res = yield call(getHscodeById, { ...R.omit(['onSuccess', 'onFail'])(payload) });
                if(res && res.data.resultCode === "ok"){
                    yield put({
                        type: 'refreshState',
                        payload: {
                            dataSource: res.data.content
                        }
                    });
                } else {
                    if(payload.onFail) payload.onFail();
                }
            } else {
                yield put({
					type: 'refreshState',
					payload: {
						dataSource: {}
					}
				});
            }
        },

        /**
         * 修改商品编码
         * 
         * @param call
         * @param put
         * @param select
         */
        *updateHscode({ payload },{ call, put }){
            const res = yield call(updateHscode, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if(res && res.data.resultCode === "ok"){
                message.info("海关商品码修改成功！重新登录后生效");
                if(payload.onSuccess) payload.onSuccess();
            } else {
                if(payload.onFail) payload.onFail();
            }
        },

      /**
         * 增加商品编码
         * 
         * @param call
         */
        *addHscode({ payload },{ call }){
            const res = yield call(addHscode, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if(res && res.data.resultCode === "ok"){
                message.info("海关商品码新增成功！");
                if(payload.onSuccess) payload.onSuccess();
            } else {
                if(payload.onFail) payload.onFail();
            }
        },

    }
};
